<!-- 产品数据统计 -->
<template>
  <div class="ProductDataStatistics">
    <div class="operate card">
      <!-- <div >时间维度:</div> -->
      <div class="timedimension">
        <el-select style="width:70px" v-model="t_value" @change="t_change">
          <el-option v-for="item in options_t_type" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </div>
      <!-- <div>时间区间:</div> -->
      <div class="picker">
        <el-date-picker style="width:180px" v-model="s_time" :editable="false" :clearable="false" :type="d_type" :format="v_type" :value-format="v_type" :picker-options="pickerOptions"></el-date-picker>
      </div>
      <div class="hidden-xs-only">
        <i class="el-icon-minus"></i>
      </div>
      <div class="picker">
        <el-tooltip :content="content" placement="top">
          <el-date-picker style="width:180px" v-model="e_time" :editable="false" :clearable="false" :type="d_type" :format="v_type" :value-format="v_type" :picker-options="pickerOptions"></el-date-picker>
        </el-tooltip>
      </div>
      <div>
        <el-select style="width:150px" v-model="workshop_value">
          <el-option v-for="item in workshop" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" :disabled="!workshop_value" @click="t_button">查询</el-button>
      </div>
      <div class="export">
        <el-tooltip content="导出" placement="top">
          <el-button type :disabled="!workshop_value" @click="t_export">
            <i style="color:#0780EDfont-size:20px" class="iconfont icon-daochu"></i>
          </el-button>
        </el-tooltip>
      </div>
    </div>
    <el-row class="card" :gutter="0">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-loading="table_loading">
        <el-table class="table_title" empty-text=" ">
          <el-table-column label="产品名称" align="center"></el-table-column>
          <el-table-column label="车间" align="center"></el-table-column>
          <el-table-column label="产量" align="center"></el-table-column>
          <el-table-column label="核算单位" align="center"></el-table-column>
        </el-table>
        <div v-for="(item,index) in tableData" :key="index" class="table">
          <el-table :show-header="false" show-summary empty-text=" " :data="item.dataStatisticsItem">
            <el-table-column label="产品名称" align="center" prop="productName"></el-table-column>
            <el-table-column label="车间" align="center" prop="workshopName"></el-table-column>
            <el-table-column label="产量" align="center" prop="number"></el-table-column>
            <el-table-column label="核算单位" align="center" prop="weight"></el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import day from "@/utils/day.js"
import Mtitle from '@/components/Mtitle'
import tb from '@/components/table'
import './index.scss'
export default {
  components: {
    Mtitle,
    tb
  },
  watch: {
  },
  data() {
    return {
      content: '查询时间跨度最大为24小时',
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      workshop_value: '',
      workshop: [],
      tableData: [
        {
          dataStatisticsItem: []
        }
      ],
      // 初始开始时间
      s_time: "",
      // 初始结束时间
      e_time: "",
      // 初始日期格式
      v_type: "yyyy-MM-dd HH:mm",
      // 初始显示类型
      d_type: "datetime",
      // 日   月  年 
      t_value: 1,
      options_t_type: [
        {
          value: 1,
          label: "日"
        },
        {
          value: 2,
          label: "月"
        },
        {
          value: 3,
          label: "年"
        }
      ],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      // 获取年月日初始化时间对象
      table_loading: true,
      export_s_time: '',
      export_e_time: '',
      export_value: '',
      export_workshop_value: '',
      export_pageNum: '',
      export_pageSize: ''
    }
  },
  computed: {
  },
  watch: {},
  mounted() {
  },
  created() {
    // 初始化默认时间
    this.s_e = day.init_value(this.d_type)
    this.s_time = this.s_e.s
    this.e_time = this.s_e.e
    this.export_s_time = this.s_time
    this.export_e_time = this.e_time
    this.export_value = this.t_value
    //获取生产线数据
    this.getqueryProductionDepartment()
  },
  methods: {
    getqueryProductionDepartment() {
      var fd = new FormData()
      fd.append("companyId", sessionStorage.getItem("custId"))
      this.$axios
        .post(
          this.Eelec + "/web/productDataStatistics/queryProductionDepartment",
          fd
        )
        .then(res => {
          if (res.data.code === 200) {
            if (res.data.data && res.data.data.length) {
              this.workshop = res.data.data
              this.workshop_value = res.data.data[0].id
              this.getqueryProductDataStatisticsInfo()
              this.export_s_time = this.s_time
              this.export_e_time = this.e_time
              this.export_value = this.t_value
              this.export_pageNum = this.page.pageNum
              this.export_pageSize = this.page.pageSize
              this.export_workshop_value = res.data.data[0].id
            } else {
              this.table_loading = false
            }

          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    async getqueryProductDataStatisticsInfo() {
      const res = await this.$axios.post(
        this.Eelec +
        "/web/productDataStatistics/queryProductDataStatisticsInfo",
        {
          custId: sessionStorage.getItem("custId"),
          startDate: this.s_time,
          endDate: this.e_time,
          pageNum: this.page.pageNum,
          pageSize: this.page.pageSize,
          timeDimension: this.t_value === 2 ? 2 : (this.t_value === 1 ? 3 : 1),
          workShopId: this.workshop_value
        }
      )
      this.table_loading = false
      if (res.data.code === 200) {
        if (res.data.data.data.length) {
          this.tableData = res.data.data.data
          this.page.total = res.data.data.totalCount
        } else {
          this.tableData = [{
            dataStatisticsItem: []
          }]
          this.page.total = 0
        }
      }
    },
    t_export() {
      this.$axios
        .post(this.Eelec + "/web/productDataStatistics/getUploadExcel", {
          custId: sessionStorage.getItem("custId"),
          pageNum: this.export_pageNum,
          pageSize: this.export_pageSize,
          startDate: this.export_s_time,
          endDate: this.export_e_time,
          timeDimension: this.export_value === 2 ? 2 : (this.t_value === 1 ? 3 : 1),
          workShopId: this.export_workshop_value
        })
        .then(res => {
          if (res.data.code === 200) {
            if (res.data.data != null) {
              this.expShow = false
              window.location.href = res.data.data.path
              this.$message({
                message: "导出成功",
                type: "success"
              })
            }
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 点击查询
    t_button() {
      day.s_e(this.s_time, this.e_time, this.t_value)
        .then(() => {
          this.export_s_time = this.s_time
          this.export_e_time = this.e_time
          this.export_value = this.t_value
          this.export_pageNum = this.page.pageNum
          this.export_pageSize = this.page.pageSize
          this.export_workshop_value = this.workshop_value
          console.log("可以调方法了")
          this.table_loading = true
          this.getqueryProductDataStatisticsInfo()
        })
        .catch(() => { })
    },
    // 切换年月日
    t_change(n) {
      if (n === 1) {
        this.content = '查询时间跨度最大为24小时'
      }
      if (n === 2) {
        this.content = '查询时间跨度最大为31天'
      }
      if (n === 3) {
        this.content = '查询时间跨度最大为12个月'
      }
      this.v_type = day.change_type(n).v
      this.d_type = day.change_type(n).d
      this.s_e = day.init_value(this.d_type)
      this.s_time = this.s_e.s
      this.e_time = this.s_e.e
    }
  }
}
</script>
 <style scoped lang='scss'>
</style>
